<html>
<head>
    <meta charset="utf-8">

    <!-- Load the editor sdk from the local folder or https://cloud.squidex.io/scripts/editor-sdk.js -->
    <script src="editor-sdk.js"></script>

    <link rel="stylesheet" type="text/css" href="https://cloud.squidex.io/styles.css">
    <style>
        body {
            background-color: white;
        }

        textarea {
            height: 300px;
        }
    </style>
</head>

<body style="margin: 0px; padding: 0px;">
	<div class="mb-2">
		<button class="btn btn-outline-secondary" id="expanded">
			Toggle Expanded
		</button>
		<button class="btn btn-outline-secondary" id="fullscreen">
			Toggle Fullscreen
		</button>
	</div>

	<div>
		<textarea class="form-control" id="editor"></textarea>
	</div>

	<script>
		var element = document.getElementById('editor');

        // When the field is instantiated it notifies the UI that it has been loaded.
        // 
        // Furthermore it sends the current size to the parent.
		var field = new SquidexFormField();

		document.getElementById('expanded').addEventListener('click', function () {
			field.toggleExpanded();
        });

        document.getElementById('fullscreen').addEventListener('click', function () {
            field.toggleFullscreen();
        });
		
		field.onValueChanged(function (value) {
			if (value) {
				element.value = JSON.stringify(value);
			} else {
				element.value = '';
			}
		});

		field.onDisabled(function (disabled) {
			element.disabled = disabled;
		});

		element.addEventListener('change', function ()  {
			var value = element.value;

			if (value) {
				field.valueChanged(JSON.parse(value));
			} else {
				field.valueChanged(undefined);
			}
		});
    </script>
</body>
</html>